package com.dx.as;

import android.app.Activity;
import android.os.Build;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RelativeLayout;

import com.dx.as.common.log.LogUtil;

/**
 * Created by jwaoo on 2017/11/24.
 */

public class TabTools {

    private RadioButton[] radioButtons;
    private View line;
    private int bigSize = 23,smallSize = 16;
    private OnTabClickListener listener;
    public TabTools(OnTabClickListener onTabClickListener) {
        this.listener = onTabClickListener;
    }

    public void initView(View view,String [] values){
        radioButtons = new RadioButton[]{(RadioButton)(view.findViewById(R.id.radio_1)),(RadioButton)(view.findViewById(R.id.radio_2)),(RadioButton)(view.findViewById(R.id.radio_3))};
        for (int i = 0; i < radioButtons.length; i ++){
            radioButtons[i].addOnLayoutChangeListener(onLayoutChangeListener);
            radioButtons[i].setOnClickListener(onClickListener);
            radioButtons[i].setText(values[i]);
        }
        line = view.findViewById(R.id.line);
        radioButtons[0].setTextSize(bigSize);
    }


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int index = Integer.parseInt(v.getTag().toString());
            for (int i = 0; i < radioButtons.length; i ++){
                radioButtons[i].setTextSize(i == index ? bigSize : smallSize);
                radioButtons[i].invalidate();
            }
            listener.onTabClick(index);
        }
    };

    private int sum = 0;
    Handler handler = new Handler(){};
    private View.OnLayoutChangeListener onLayoutChangeListener = new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            if (Build.VERSION.SDK_INT > 16){
                sum ++;
                int tag = Integer.parseInt(v.getTag().toString());
                if ((right - left) > (oldRight - oldLeft) && (sum > 3 || tag == 0)){
                    final int start = right - (right - left)/2 - line.getMeasuredWidth()/2;
                    Log.d("layout",line.getLeft()+"  :  "+line.getRight()+"  :  "+line.getTop()+"  :  "+line.getBottom());
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            setLayoutX(line,start);
                        }
                    },50);

//                    Log.d("layout",v.getTag().toString()+"   start = "+start +"   line : "+line.getMeasuredWidth()+"   "+line.getRight()+"  " +line.getLeft());
//                    line.layout(start,line.getTop(),start+line.getRight(),line.getBottom());
                }
//                Log.d("layout","view.getRight() = "+view.getRight() +"  view.getMinimumWidth() = "+view.getMeasuredWidth()+"   line.getMinimumWidth() = "+line.getMeasuredWidth() + "  start = "+start);

            }

        }
    };

    /*
 * 设置控件所在的位置X，并且不改变宽高，
 * X为绝对位置，此时Y可能归0
 */
    public static void setLayoutX(View view,int x){
        ViewGroup.MarginLayoutParams margin=new ViewGroup.MarginLayoutParams(view.getLayoutParams());
        margin.setMargins(x,0, 0, 0);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(margin);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        view.setLayoutParams(layoutParams);
//        view.on
    }

    public interface OnTabClickListener {
        void onTabClick(int option);
    }

}
